import 'package:get/get.dart';
import 'package:hygf/http/file_transfer/down_cap.dart';

import '../../../../bean/CustomerList.dart';
import '../../../../bean/add_grid_net_bean.dart';
import '../../../../config/ApiConfig.dart';
import '../../../../config/GlobalConfig.dart';
import '../../../../http/DioClient.dart';
import '../../../../http/core/ResponseData.dart';
import '../../../../util/HDNavigator.dart';
import '../../../../util/LoadingUtils.dart';
import '../../../../view/image_picker_widget.dart';
import '../../allnet/net_record/logic.dart';
import '../../allnet/un_net/logic.dart';

import 'state.dart';

class OperationDetailLogic extends GetxController {
  final OperationDetailState state = OperationDetailState();

  @override
  void onReady() {
    super.onReady();
    initImgUrl();
  }

  Future<void> submit(int flag) async {
    state.addOperation.id = state.operationBean.id;
    state.addOperation.customerInfoId = state.operationBean.customerInfoId;
    state.addOperation.customerName = state.operationBean.customerName;
    state.addOperation.phoneNumber = state.operationBean.phoneNumber;
    state.addOperation.address = state.operationBean.address;
    state.addOperation.buildingAddress = state.operationBean.buildingAddress;
    state.addOperation.faultDescription = state.operationBean.faultDescription;
    state.addOperation.described = state.operationBean.described;
    state.addOperation.status = flag;
    if(check()){
      if(state.beforeController.data.isNotEmpty){
        if((state.addOperation.repairBefore??[]).isEmpty){
          state.addOperation.repairBefore = [];
        }
        for(var item in state.beforeController.data){
          state.addOperation.repairBefore!.add(item.id??0);
        }
      }
      if(state.repairInController.data.isNotEmpty){
        if((state.addOperation.repairIn??[]).isEmpty){
          state.addOperation.repairIn = [];
        }
        for(var item in state.repairInController.data){
          state.addOperation.repairIn!.add(item.id??0);
        }
      }
      if(state.afterController.data.isNotEmpty){
        if((state.addOperation.repairAfter??[]).isEmpty){
          state.addOperation.repairAfter = [];
        }
        for(var item in state.afterController.data){
          state.addOperation.repairAfter!.add(item.id??0);
        }
      }
      ResponseData response = await DioClient().doPost(ApiConfig.saveOperationUrl, data: state.addOperation);
      if (response.isok) {
        if(response.code == "0"){
          LoadingUtils.showToast(response.msg??"提交成功");
          Get.put(UnNetLogic()).callRefresh();
          HDNavigator.close(result: "refresh");
        }else {
          LoadingUtils.showToast(response.msg??"提交失败");
        }
      } else {
        LoadingUtils.showToast(response.msg??"提交失败");
      }
    }
  }

  bool check(){
    return true;
  }

    Future<void> initImgUrl() async {
      if((state.operationBean.repairBefore??"").isNotEmpty){
        List<ImagePickerData> edpData = await DioClient().requestImgUrl(state.operationBean.repairBefore);
        state.beforeController.addImages(edpData);
      }
      if((state.operationBean.repairIn??"").isNotEmpty){
        List<ImagePickerData> otherData = await DioClient().requestImgUrl(state.operationBean.repairIn);
        state.repairInController.addImages(otherData);
      }
      if((state.operationBean.repairAfter??"").isNotEmpty){
        List<ImagePickerData> applicationProcessData = await DioClient().requestImgUrl(state.operationBean.repairAfter);
        state.afterController.addImages(applicationProcessData);
      }
      update();
    }

  void closePage() {
    HDNavigator.close();
  }

}
